package edu.gatech.cs2340.mule.model;
import java.awt.Point;
import java.io.Serializable;


/**
 * 
 * This class will be used to keep track of the layout of the game map.
 * Two kinds of Maps can be produced: a map with the default layout or a
 * randomly generated layout.
 * 
 * @author Keegan Nesbitt
 *
 */
public class Map implements Serializable
{
	private static final long serialVersionUID = 504363705678106708L;

	private static char[][] defaultMap =   {{'P', 'P', '1', 'P', 'R', 'P', '3', 'P', 'P'},
											{'P', '1', 'P', 'P', 'R', 'P', 'P', 'P', '3'},
											{'3', 'P', 'P', 'P', 'T', 'P', 'P', 'P', '1'},
											{'P', '2', 'P', 'P', 'R', 'P', '2', 'P', 'P'},
											{'P', 'P', '2', 'P', 'R', 'P', 'P', 'P', '2'}};
	private MapTile[][] mapGrid;
	private String mapType;
	
	/**
	 * Creates the map for the game with the default map layout
	 * 
	 */
	public Map()
	{
		mapType = "Classic";
		
		mapGrid = new MapTile[defaultMap.length][defaultMap[0].length];
		for(int row = 0; row < mapGrid.length; row++) {
			for(int column = 0; column < mapGrid[0].length; column++) {
				switch(defaultMap[row][column]) {
					case 'P':
						mapGrid[row][column] = new Plain();
						break;
					case 'R':
						mapGrid[row][column] = new River();
						break;
					case 'T':
						mapGrid[row][column] = new Town();
						break;
					case '1':
						mapGrid[row][column] = new MountainOne();
						break;
					case '2':
						mapGrid[row][column] = new MountainTwo();
						break;
					case '3':
						mapGrid[row][column] = new MountainThree();
						break;
				} // end switch statement
			} // end column for loop
		} // end row for loop
		
		getTown().getLandOffice().stockInitialProperties(this);
	} // end Map constructor
	
	/**
	 * Creates a map based on the given type (usually random)
	 * @param type the map type to generate
	 */
	public Map(String type) {
		mapType = type;
		if(type.equals("Random")) {
			//generate random map
		}
	}
	
	/**
	 * 
	 * @param p the point whose map tile is to be returned
	 * @return The MapTile at the specified point
	 */
	public MapTile getMapTileAt(Point p)
	{
		return mapGrid[ (int) p.getX()][ (int) p.getY()];
	} // end getMapTileAt method
	
	/**
	 *  Returns the town in the map
	 *  
	 * @return the MapTile that is the Town
	 */
	public Town getTown()
	{
		for(int row = 0; row < mapGrid.length; row++) {
			for(int column = 0; column < mapGrid[0].length; column++) {
				if(mapGrid[row][column] instanceof Town) {
					return (Town) mapGrid[row][column];
				}
			}
		}
			
		return null; //return null if there is no Town
	} // end getTown method
	
	/**
	 * Finds the coordinates of the Town
	 * 
	 * @return The point the town is located at
	 */
	public Point findTown()
	{
		for(int row = 0; row < mapGrid.length; row++) {
			for(int column = 0; column < mapGrid[0].length; column++) {
				if(mapGrid[row][column] instanceof Town) {
					return new Point(row,column);
				}
			}
		}
			
		return null; //return null if there is no Town
	} // end findTown method
	
	/**
	 * 
	 * @return The width of the map
	 */
	public int getMapWidth() {
		return mapGrid[0].length;
	} // end getMapWidth method
	
	/**
	 * 
	 * @return The height of the map
	 */
	public int getMapHeight() {
		return mapGrid.length;
	} // end getMapHeight method
	
	/**
	 * 
	 * @return The type of the map (Random, standard,...)
	 */
	public String getMapType() {
		return mapType;
	}
} // end Map class
